package com.ihrm.common.interceptor;

import com.alibaba.fastjson.TypeReference;
import com.ihrm.common.exception.RRException;
import com.ihrm.common.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//@Component
public class JwtInterceptor extends HandlerInterceptorAdapter {

    @Autowired
    private JwtUtils jwtUtils;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
////        return super.preHandle(request, response, handler);
//        String token = request.getHeader("Authorization");
//        if (StringUtils.isBlank(token)) {
//            token = request.getParameter("Authorization");
//        }
//        if (StringUtils.isBlank(token)) {
//            throw new RRException("未登录,请先登录或者注册");
//        }
//        Claims claims = jwtUtils.parseJwt(token);
//        if (claims != null) {
//            Object apis = claims.get("apis");
//            request.setAttribute("user_claims", claims);
//            return true;
//        }
//        return false;
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        super.afterCompletion(request, response, handler, ex);
    }
}
